import {getRequest} from "./api";



export const initMenu = (router,store)=>{
    if(store.state.routes.length>0){
        return;
    }
    if(window.sessionStorage.getItem("userId"))
    getRequest('/sys/resource/queryResources?t='+new Date()+'&userId='+window.sessionStorage.getItem("userId")).then(resp=>{
        console.log(resp.result)
        let formatRoutes = formateRoutes(resp.result)
        // 添加到路

        router.addRoutes(formatRoutes)

        store.commit('initRoutes',formatRoutes)
    })

}
export const formateRoutes=(data)=>{
    let formateRouters=[]
    data.forEach(router=>{
        let{
            resourceurl,
            resourcename,
            children,
            component,
            icon
        } = router;
        if(children && children instanceof Array){
            // 递归
            children = formateRoutes(children)
        }
        let fmtRouter={
            path:'/'+resourceurl,
            name:resourcename,
            iconCls:icon,
            children:children,
            component(resolve){
                require(['../views/'+component+'.vue'],resolve);
            }
        }
        formateRouters.push(fmtRouter)
    });
    return formateRouters;
}